package cn.tj212.xiaoyin.wechat.wxapi.service.impl;

import cn.tj212.xiaoyin.common.storage.QiniuStorage;
import cn.tj212.xiaoyin.core.course.domain.Course;
import cn.tj212.xiaoyin.core.course.domain.CourseQueryDto;
import cn.tj212.xiaoyin.core.course.service.ICourseService;
import cn.tj212.xiaoyin.wechat.wxapi.process.MsgType;
import cn.tj212.xiaoyin.wechat.wxapi.process.MsgXmlUtil;
import cn.tj212.xiaoyin.wechat.wxapi.service.MyService;
import cn.tj212.xiaoyin.wechat.wxapi.vo.Article;
import cn.tj212.xiaoyin.wechat.wxapi.vo.MsgNews;
import cn.tj212.xiaoyin.wechat.wxapi.vo.MsgRequest;
import cn.tj212.xiaoyin.wechat.wxapi.vo.MsgResponseNews;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  业务消息处理
 *  开发者根据自己的业务自行处理消息的接收与回复
 */
public class MyServiceImpl implements MyService {

    @Autowired
    private ICourseService courseService;

    /**
     * 处理消息
     *
     * @param msgRequest 接收到的消息
     * @param contextUri
     * @return
     */
    @Override
    public String processMsg(MsgRequest msgRequest, String contextUri) {
        List<MsgNews> msgNews=new ArrayList<MsgNews>();
        //数据库中的课程
        this.prepareCourseNews(msgNews,contextUri);
        return MsgXmlUtil.newsToXml(getMsgResponseNews(msgRequest,msgNews));
    }

    private void prepareCourseNews(List<MsgNews> msgNews, String contextUri) {
        CourseQueryDto queryEntity=new CourseQueryDto();
        queryEntity.setStart(0);
        queryEntity.setCount(3);
        List<Course> list=courseService.queryList(queryEntity);
        for (Course c:list
             ) {
            MsgNews news=new MsgNews();
            news.setTitle(c.getName());
            news.setBrief(c.getBrief());
            news.setMsgType(MsgType.News.name());
            if (StringUtils.isNotEmpty(c.getPicture())){
                news.setPicpath(QiniuStorage.getUrl(c.getPicture()));
            }else {
                //todo
                //给一个默认图片
                news.setPicpath("");
            }
            news.setUrl(contextUri+"/course/video.html?id="+c.getId());
            msgNews.add(news);
        }
    }

    /**
     * 构造一条图文消息
     * @param msgRequest
     * @param msgNews
     * @return
     */
    private MsgResponseNews getMsgResponseNews(MsgRequest msgRequest,List<MsgNews> msgNews){
        if (msgNews!=null&&msgNews.size()>0){
            MsgResponseNews responseNews=new MsgResponseNews();
            responseNews.setToUsername(msgRequest.getFromUsername());
            responseNews.setFromUsername(msgRequest.getToUsername());
            responseNews.setMsgType(MsgType.News.toString());
            responseNews.setCreateTime(new Date().getTime());
            responseNews.setArticleCount(msgNews.size());
            List<Article> articles=new ArrayList<Article>(msgNews.size());
            for (MsgNews n:msgNews
                 ) {
                Article article=new Article();
                article.setTitle(n.getTitle());
                article.setPicUrl(n.getPicpath());
                if (StringUtils.isEmpty(n.getFromurl())){
                    article.setUrl(n.getUrl());
                }else {
                    article.setUrl(n.getFromurl());
                }
                article.setDescription(n.getBrief());
                articles.add(article);
            }
            responseNews.setArticles(articles);
            return responseNews;
        }else {
            return null;
        }
    }
}
